package com.tmpt.test;

/*import org.apache.log4j.PropertyConfigurator;
import org.olap4j.*;
import org.olap4j.metadata.Member;*/

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author yanghs
 * @Description:
 * @date 2018/4/21 11:49
 */
public class Mondrian {
    /*public static void main(String[] args) throws FileNotFoundException {

        OlapConnection conn = null;
        try {
            //InputStream is = new BufferedInputStream(new FileInputStream("G:\\mycode\\mondriandemo\\src\\main\\javacode\\com\\yanghs\\test\\log4j.properties"));
            //PropertyConfigurator.configure( is);
            conn= getConnection(
                    //URL协议
                    "jdbc:mondrian:"
                            //连接数据源的JDBC连接
                            + "Jdbc='jdbc:mysql://localhost:3307/mdx?user=root&password=laoliou&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8';"
                            //数据模型文件
                            + "Catalog=E:\\files1\\a\\mondrian\\FoodMart1.xml;"
                            //连接数据源用到的驱动
                            + "JdbcDrivers=com.mysql.jdbc.Driver;");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //查询语句
        //String mdx="SELECT {[Measures].[numb]} ON 0,{[proType].[proId].members} on 1 FROM [Sales]";
        //String mdx="select {[Measures].SUMNum} on columns, {([Customer].[allCustomer])}on rows from [qiu-cube]";
        String mdx="with member [Measures].[ROI] as '(([Measures].[Store Sales] - [Measures].[Store Cost]) / [Measures].[Store Cost])', format_string = IIf((((([Measures].[Store Sales] - [Measures].[Store Cost]) / [Measures].[Store Cost]) * 100.0) > 150.0), \"|#.00%|arrow='up'\", IIf((((([Measures].[Store Sales] - [Measures].[Store Cost]) / [Measures].[Store Cost]) * 100.0) < 150.0), \"|#.00%|arrow='down'\", \"|#.00%|arrow='none'\"))\n" +
                "select {[Measures].[ROI], [Measures].[Store Cost], [Measures].[Store Sales]} ON columns,\n" +
                "  {[Product].[All Products]} ON rows\n" +
                "from [Sales]\n" +
                "where [Time].[2014]";
        CellSet cs= null;
        try {
            cs = getResultSet(mdx, conn);
        } catch (OlapException e) {
            e.printStackTrace();
        }
        *//*遍历结果*//*
        if(cs.getAxes().size()>1){
            for (Position row : cs.getAxes().get(1)) {
                for (Position column : cs.getAxes().get(0)) {
                    for (Member member : row.getMembers()) {
                        System.out.println("rows:"+member.getUniqueName());
                    }
                    for (Member member : column.getMembers()) {
                        System.out.println("columns:"+member.getUniqueName());
                    }
                    final Cell cell = cs.getCell(column, row);
                    System.out.println("values:"+cell.getValue());
                    System.out.println();
                }
            }
        }else{
            for(Position column:cs.getAxes().get(0))
            {
                for(Member member:column.getMembers()){
                    System.out.println("columns:"+member.getUniqueName());
                }
                Cell cell=cs.getCell(column);
                System.out.print("values:"+cell.getValue());
                System.out.println();
            }
        }

    }

    *//**
     * 得到连接
     * @param url
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     *//*
    public static OlapConnection getConnection(String url) throws ClassNotFoundException, SQLException {
        Class.forName("mondrian.olap4j.MondrianOlap4jDriver");
        Connection connection = DriverManager.getConnection(url);
        return connection.unwrap(OlapConnection.class);
    }

    *//**
     * 得打查询的结果
     * @param mdx
     * @param conn
     * @return
     * @throws OlapException
     *//*
    public static CellSet getResultSet(String mdx, OlapConnection conn) throws OlapException {
        OlapStatement statement = conn.createStatement();
        CellSet cellSet = statement.executeOlapQuery(mdx);
        return cellSet;
    }*/
}
